package com.leetcode.LeetCode精选TOP面试题;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 覃国强
 * @date 2022-05-24 19:02
 */
public class 题库_0049_字母异位词分组 {

  static
  class Solution {
    public List<List<String>> groupAnagrams(String[] strs) {
      if (strs == null || strs.length == 0) {
        return new ArrayList<>();
      }
      Map<String, List<String>> map = new HashMap<>();
      for (String str : strs) {
        char[] chars = str.toCharArray();
        Arrays.sort(chars);
        String key = new String(chars);
        if (!map.containsKey(key)) {
          map.put(key, new ArrayList<>());
        }
        List<String> values = map.get(key);
        values.add(str);
      }
      return new ArrayList<>(map.values());
    }
  }

}
